/**
 * 
 */
package cn.gov.msa.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;

/**
 * @author Administrator
 *
 */
public class ObjectUtil {
	/**
	 * 
	* @Title: FileUtil.java 
	* @Package cn.gov.msa.util 
	* @Description: TODO(时间格式转字条串，年-月-日) 
	* @author 何宇江  
	* @date 2017年8月1日 上午9:57:47
	 */
	public static String ConvertDataToString(Date date) throws Exception {
		String result = "";
		
	    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
	    result=sdf.format(date); 
		
		return result;
	}
	
	
	/**
	 * 
	* @Title: ObjectUtil.java 
	* @Package cn.gov.msa.util 
	* @Description: TODO(去除值为空的键值) 
	* @author 何宇江  
	* @date 2017年8月1日 下午5:39:41
	 */
	public static JSONObject DeleteJSONNull(JSONObject param) throws Exception {
		
		JSONObject result = JSONObject.fromObject(param.toString());
		
		for (Iterator iter = param.keys(); iter.hasNext();) { //先遍历整个  对象  
		    String key = (String)iter.next();  
		    if("".equals(param.getString(key)) || param.get(key) instanceof JSONNull) {
		    	result.remove(key);
		    }
		} 
		
		return result;
	}
	
	/**
	 * 
	* @Title: ObjectUtil.java 
	* @Package cn.gov.msa.util 
	* @Description: TODO(去除值为空的键值) 
	* @author 何宇江  
	* @date 2017年8月1日 下午5:39:41
	 */
	public static JSONArray DeleteJSONNull(JSONArray params) throws Exception {
		JSONArray results = new JSONArray();
		
		for (Object object : params) {
			JSONObject param = JSONObject.fromObject(object);			
			JSONObject result = JSONObject.fromObject(object);
			
			for (Iterator iter = param.keys(); iter.hasNext();) { //先遍历整个  对象  
			    String key = (String)iter.next();  
			    if("".equals(param.getString(key)) || param.get(key) instanceof JSONNull) {
			    	result.remove(key);
			    }
			} 
			
			results.add(result);
		}
		
		return results;
	}
	
	public static void main(String[] args) throws Exception {
		String ss = "{\"acceptdpt\":\"\",\"acceptdptname\":\"\",\"acceptorg\":\"\",\"acceptorgname\":\"\",\"acceptstaff\":\"\",\"acceptstaffname\":\"\",\"activitystep\":\"\",\"activitystepcode\":\"\",\"applyid\":\"5598F720D45045F2E050007F010040EF\",\"applyno\":\"HSZW201700000066\",\"applyremark\":\"备注\",\"applytime\":\"2017-08-01\",\"approvedate\":null,\"approveresult\":\"\",\"approveresultcode\":\"\",\"approvestaff\":\"\",\"areacode\":\"\",\"builddate\":null,\"callsign\":\"\",\"cardno\":\"\",\"checkno\":\"\",\"company\":\"\",\"companyid\":\"\",\"contactaddr\":\"\",\"deadweight\":0,\"email\":\"\",\"enginepower\":0,\"finishlimittime\":null,\"flagcode\":\"\",\"grosstonnage\":0,\"imo\":\"\",\"initregno\":\"\",\"issendmsg\":0,\"itemname\":\"通航水域岸线安全使用许可\",\"itemno\":\"1001\",\"itemtypecode\":\"01\",\"linecode\":\"\",\"markcode\":\"\",\"mmsi\":0,\"mobilephone\":\"\",\"mouldedbreadth\":0,\"mouldeddepth\":0,\"nettonnage\":0,\"nodename\":\"申请单节点名\",\"nodeno\":\"AC_0001\",\"nopasscause\":\"\",\"objectname\":\"\",\"operator\":\"\",\"operatoraddress\":\"\",\"operatorcorporation\":\"\",\"operatortelephone\":\"\",\"orgcode\":\"010000\",\"orgname\":\"上海海事局\",\"overalllength\":0,\"owner\":\"\",\"owneraddress\":\"\",\"ownercorporation\":\"\",\"ownertelephone\":\"\",\"portcode\":\"\",\"proposer\":\"测试\",\"proposerid\":\"123456\",\"ratedseat\":0,\"regno\":\"\",\"satisfaction\":0,\"shipid\":\"\",\"shipidnumber\":\"\",\"shipname\":\"\",\"shipnameen\":\"\",\"sourcechannelcode\":1,\"syscreated\":null,\"syscreatedby\":\"\",\"syscreatedbyname\":\"\",\"sysdept\":\"\",\"sysdeptname\":\"\",\"syslastupd\":null,\"syslastupdby\":\"\",\"sysorg\":\"\",\"sysorgname\":\"\",\"telephone\":\"\",\"typecode\":\"\"}";
		JSONObject param = JSONObject.fromObject(ss);
		
		param = ObjectUtil.DeleteJSONNull(param);
		
		System.out.println(">>>" + param);
	}
}
